﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FTBS.Models.DAO;
using FTBS.Models.Entities;

namespace FTBS.Models.BLO
{
    public class TicketBLO
    {
        private TicketDAO _ticketDao = new TicketDAO();
        private ScheduleDAO _scheduleDao = new ScheduleDAO();

        /// <summary>
        /// check status of created ticket
        /// </summary>
        /// <param name="scheduleId"></param>
        /// <param name="chairId"></param>
        /// <returns>created: true; not created: false</returns>
        /// <author>hungph</author>
        public bool CheckCreatedTicket(int scheduleId, int chairId)
        {
            if (_ticketDao.CheckCreatedTicket(scheduleId, chairId) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool CheckTicketToCreate(int scheduleId, int chairId)
        {
            if (_ticketDao.CheckTicketToCreate(scheduleId, chairId) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int GetTicketId(int scheduleId, int chairId)
        {
            return _ticketDao.GetTicketId(scheduleId, chairId);
        }

        public Ticket GetTicket(int scheduleId, int chairId)
        {
            return _ticketDao.GetTicket(scheduleId, chairId);
        }

        public bool UpdateTicket(int ticketId, bool isDelete)
        {
            var result = _ticketDao.UpdateTicket(ticketId, isDelete);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdatePriceTicket(int ticketId, double price, bool isDeleted)
        {
            var result = _ticketDao.UpdatePriceTicket(ticketId, price, isDeleted);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool CreateTickets(List<Ticket> tickets)
        {
            var result = _ticketDao.AddTickets(tickets);
            if (result == tickets.LongCount())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool CreateTicket(Ticket ticket)
        {
            var result = _ticketDao.AddTicket(ticket);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Ticket GetTicketById(int ticketId)
        {
            var ticket = _ticketDao.GetTicketById(ticketId);
            return ticket;
        }

        public int CountBookedTicketsByScheduleId(int scheduleId)
        {
            var count = _ticketDao.CountBookedTicketsByScheduleId(scheduleId);
            return count;
        }

        public bool CheckScheduleHasTickets(int scheduleId)
        {
            var numberOfTickets = _ticketDao.CountTicketsByScheduleId(scheduleId);
            if (numberOfTickets > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public List<Ticket> getListTicketById(int scheduleId)
        {
            //var scheduleId =_scheduleDao.GetScheduleId(filmId,roomId,showtimeId,theaterId);
            return _ticketDao.getListTicket(scheduleId);
        }

        /// <summary>
        /// Check Price Ticket of theaer to create ticket
        /// </summary>
        /// <param name="scheduleId"></param>
        /// <returns>true: can create ticket; false: can't create ticket</returns>
        /// <author>hungph</author>
        public bool CheckPriceToCreateTicket(int scheduleId)
        {
            return _ticketDao.CheckPriceToCreateTicket(scheduleId);
        }


        /// <summary>
        /// Check enough Price Ticket of theaer to create ticket
        /// </summary>
        /// <param name="scheduleId"></param>
        /// <returns>true: can create ticket; false: can't create ticket</returns>
        /// <author>hungph</author>
        public bool CheckEnoughPriceTicketToCreateTicket(int scheduleId)
        {
            return _ticketDao.CheckEnoughPriceTicketToCreateTicket(scheduleId);
        }

        /// <summary>
        /// Update PurePrice in Ticket when update Price in PriceTicket table
        /// </summary>
        /// <param name="priceTicket"></param>
        public void UpdateTicketPurePrice(PriceTicket priceTicket)
        {
            _ticketDao.UpdateTicketPurePrice(priceTicket);
        }

        public bool UpdateSelectdTickets(List<Ticket> listNotBookTickets, List<Ticket> listSelectedTickets, int theaterId)
        {
            int result = _ticketDao.UpdateSelectdTickets(listNotBookTickets, listSelectedTickets, theaterId);
            if (result == listNotBookTickets.LongCount())
            {
                return true;
            }
            return false;
        }
    }
}